package com.liuyk.commonlib.fragment;

import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.liuyk.commonlib.R;
import com.liuyk.commonlib.widget.MultiStateView;

/**
 * 带有加载状态的fragment
 * <p>
 * Created by Administrator on 2019/11/5.
 */
public abstract class BaseLoadingFragment extends BaseFragment {

    private MultiStateView mMultiStateView;
    private TextView mEmptyMessage;

    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.base_loading_fragment_layout, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mMultiStateView = view.findViewById(R.id.multi_state_view);
        mEmptyMessage = mMultiStateView.findViewById(R.id.empty_message);
        mMultiStateView.setViewState(MultiStateView.ViewState.LOADING);
        //noinspection ConstantConditions
        mMultiStateView.getView(MultiStateView.ViewState.ERROR).setOnClickListener(this);
    }

    protected void setLoadingContent(int resId) {
        final View contentView = LayoutInflater.from(getActivity()).inflate(resId, mMultiStateView, false);
        setLoadingContent(contentView);
    }

    protected void setLoadingContent(View view) {
        mMultiStateView.setViewForState(view, MultiStateView.ViewState.CONTENT);
    }

    protected void showErrorState() {
        mMultiStateView.setViewState(MultiStateView.ViewState.ERROR);
    }

    protected void showLoadingState() {
        mMultiStateView.setViewState(MultiStateView.ViewState.LOADING);
    }

    protected void showEmptyState() {
        mMultiStateView.setViewState(MultiStateView.ViewState.EMPTY);
    }

    protected void showContentState() {
        mMultiStateView.setViewState(MultiStateView.ViewState.CONTENT);
    }

    protected void setEmptyMessage(String message) {
        mEmptyMessage.setText(message);
    }

    protected void setEmptyTextColor(int resId) {
        mEmptyMessage.setTextColor(resId);
    }

    protected void setEmptyTextSize(float size) {
        mEmptyMessage.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    protected MultiStateView.ViewState getCurrentViewSate() {
        return mMultiStateView.getViewState();
    }

    protected void setEmptyView(int resId) {
        View emptyView = LayoutInflater.from(getContext()).inflate(resId, mMultiStateView, false);
        setEmptyView(emptyView);
    }

    protected void setEmptyView(View emptyView) {
        mMultiStateView.setViewForState(emptyView, MultiStateView.ViewState.EMPTY);
    }

    protected View getEmptyView() {
        return mMultiStateView.getView(MultiStateView.ViewState.EMPTY);
    }

    protected void setFailView(int resId) {
        View failView = LayoutInflater.from(getContext()).inflate(resId, mMultiStateView, false);
        setFailView(failView);
    }

    protected void setFailView(View failView) {
        mMultiStateView.setViewForState(failView, MultiStateView.ViewState.ERROR);
    }

    protected View getFailView() {
        return mMultiStateView.getView(MultiStateView.ViewState.ERROR);
    }

    protected void setLoadingView(int resId) {
        View loadingView = LayoutInflater.from(getContext()).inflate(resId, mMultiStateView, false);
        setLoadingView(loadingView);
    }

    protected void setLoadingView(View loadingView) {
        mMultiStateView.setViewForState(loadingView, MultiStateView.ViewState.LOADING);
    }

    protected View getLoadingView() {
        return mMultiStateView.getView(MultiStateView.ViewState.LOADING);
    }

    /**
     * 重新加载
     */
    protected abstract void onRetry();

    @Override
    public void onClick(View v) {
        if (v == mMultiStateView.getView(MultiStateView.ViewState.ERROR)) {
            showLoadingState();
            onRetry();
        }
    }
}
